package dt;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @title 数据库操作
 * @description 数据库操作
 * @since Java5
 */
public class JDBCUtil {

    private JDBCUtil() {
    }


    static {
        try {
            String driver = "com.ibm.db2.jcc.DB2Driver";
            // 装载驱动
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            throw new ExceptionInInitializerError(e);
        } catch (Exception ex) {
            throw new RuntimeException("database driver load error");
        }
    }

    /**
     * 获取数据库连接
     * @param url url
     * @param user 用户名
     * @param password 密码
     * @return 数据库连接
     * @throws SQLException
     */
    public static Connection getConnection(String url, String user, String password) throws SQLException {

        return DriverManager.getConnection(url, user, password);
    }


    /**
     * 释放数据库相关对象
     *
     * @param rs 结果集
     * @param st 声明
     * @param conn 连接
     */
    public static void free(ResultSet rs, Statement st, Connection conn) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (st != null) {
                    st.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                if (conn != null) {
                    try {
                        if (!conn.isClosed()) {
                            conn.close();
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        System.out.println(args.length);
        String f="/tmp/c.conf";
        if(args.length==1){
            f=args[0];
        }
        Properties properties = new Properties();
             // 使用InPutStream流读取properties文件
             BufferedReader bufferedReader = new BufferedReader(new FileReader(f));
        try {
            properties.load(bufferedReader);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String pwd=properties.getProperty("pwd");
        String user=properties.getProperty("user");
        String sql=properties.getProperty("sql");
        String url=properties.getProperty("url");
        System.out.println("url:"+url);
        System.out.println("user:"+user);
        System.out.println("pwd:"+pwd);
        System.out.println("sql:"+sql);


        try {
            conn = JDBCUtil.getConnection(url, user, pwd);
            st = conn.createStatement();
            for(int i=0;i<100;i++){
                rs = st.executeQuery(sql);
                Thread.sleep(1000);    //
                while(rs.next()) {
                    System.out.println(i+"---"+rs.getString(1));
                }
            }
            System.out.println("test for connection correct");
        } catch (SQLException e) {
            // 添加日志记录该异常
            e.printStackTrace();
            System.out.println("test for connection exception");
//        } catch (InterruptedException e) {
//            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.free(rs, st, conn);
        }

    }
}
